using System;

namespace Kollektiv.Validation
{
    /// <summary>
    /// Checks a string for not being shorter than specified.
    /// </summary>
	public class MinLengthPVA : ParameterValidatorAttribute
	{
		private int minLength = 0;
		
		public MinLengthPVA (int minLength)
		{
			this.minLength = minLength;
		}
		
		#region implemented abstract members of Kollektiv.Validation.ParameterValidatorAttribute
		public override void Validate (object value, string parameterName)
		{
			if (value is string) {
				string s = value as string;
				if (!(string.IsNullOrEmpty (s) && s.Length >= this.minLength)) {
					throw(new ParameterValidatorException (
						string.Format ("\"{0}\" has less than \"{1}\" characters", parameterName, this.minLength)));
				}
			} else {
				throw(new ParameterValidatorException (
					string.Format ("parameter \"{0}\" has to be as string for use with {1}", parameterName, this.GetType ().Name)));
			}
			
		}
		#endregion
	}
}

